﻿using Sc.Cache;
using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class DriverScoreAppealService
    {
        /// <summary>
        /// 待办事宜服务层
        /// </summary>
        private Sys_ToDoListService service = new Sys_ToDoListService();
        /// <summary>
        /// 意见数据持久层
        /// </summary>
        /// 
        private OpinionsDAL oDao = new OpinionsDAL();
        /// <summary>
        /// 评分申诉数据库层
        /// </summary>
        private DriverScoreAppealsDAO dao = new DriverScoreAppealsDAO();

        /// <summary>
        /// 保存评分申诉
        /// </summary>
        /// <param name="ent">页面数据</param>
        /// <returns></returns>
        public ReturnModel SaveData(DriverScoreAppeals ent)
        {
            ent.ApproalUsres = "USER0;";
            ent.NodeCount = 1; ent.InstanceState = 3;
            try
            {
                var bussId = dao.Add(ent);
                ReturnModel r = new ReturnModel();
                r.SourceId = bussId; r.Message = "添加成功";
                return r;
            }
            catch (Exception ex)
            {
                ReturnModel error = new ReturnModel();
                error.SourceId = 0; error.Message = ex.Message;
                return error;
            }
        }

        /// <summary>
        /// 获取评分申诉申请列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <returns></returns>
        public CommModel<DriverScoreAppeals> GetList(int userId, int pageSize, int pageIndex)
        {
            Hashtable param = new Hashtable();
            param.Add("A1", pageIndex);
            param.Add("A2", pageSize);
            param.Add("A3", "AND UserId=" + userId);
            param.Add("A4", "DriverScoreAppeals");
            param.Add("A5", "CreateTime DESC");
            var ht = CommDAO.CommPagination(param);
            DataTable dt = ht["data"] as DataTable;
            List<DriverScoreAppeals> list = new List<DriverScoreAppeals>();
            foreach (DataRow item in dt.Rows)
            {
                list.Add(dao.DataRowToModel(item));
            }
            CommModel<DriverScoreAppeals> ent = new CommModel<DriverScoreAppeals>();
            ent.Data = list;
            ent.PageCount = Convert.ToInt32(ht["PageCount"]);
            ent.RecordCount = Convert.ToInt32(ht["record"]);
            return ent;
        }
        /// <summary>
        /// 获取单个评分申诉数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <summary>
        public DriverScoreAppeals GetDetail(int id)
        {
            var tmp = this.dao.GetModel(id);
            tmp.UserName = CommService.IntanceSys().GetUserById(tmp.UserId).Name;
           // tmp.OpinionList = new OpinionsBLL().GetList("DSAS" + id);
            tmp.OpinionList = new List<Opinions>();
            if (tmp.Type == 0)
            {
                tmp.UseCarApplication = new Car_UseCarApplicationService().GetDetail(tmp.BussId);
            }
            else
            {
                tmp.Maintenance = new Car_MaintenanceService().GetDetail(tmp.BussId);
            }
            return tmp;;
        }
        /// <summary>
        /// 评分申诉审批
        /// </summary>
        /// <param name="bid">业务ID</param>
        /// <param name="userId">当前用户Id</param>
        /// <param name="opa">操作</param>
        /// <param name="todoId">待办事宜ID</param>
        /// <param name="opContext">o审批意见</param>
        /// <returns></returns>
        public ReturnModel Approal(int bid, int userId, int opa, int todoId, string opContext)
        {
            var ent = dao.GetModel(bid);
            if (ent == null) return new ReturnModel() { SourceId = 0, Message = "请上传正确的业务ID" };
            var upUsers = CommService.UpdateApproalUers(ent.ApproalUsres, userId, ent.NodeCount);
            var upNodeCount = CommService.IsUpdateNodeCount(ent.ApproalUsres, userId, ent.NodeCount);
            IList<Sys_ToDoList> list = new List<Sys_ToDoList>();
            if (opa == 1)//拒绝
            {
                ent.InstanceState = 2;
            }
            else
            {
                if (ent.NodeCount == 1)
                {
                    ent.InstanceState = 3;
                }
                else
                {
                    ent.InstanceState = 1;
                    if (upNodeCount < ent.NodeCount)
                    {
                        var nextUsers = CommService.FindNextUsers(ent.ApproalUsres, userId, ent.NodeCount);
                        var to = service.GetDetail(todoId);
                        for (int i = 0; i < nextUsers.Count; i++)
                        {
                            Sys_ToDoList todo = new Sys_ToDoList();
                            todo.ActionURL = to.ActionURL;
                            todo.CreaterName = "系统";
                            todo.Name = to.Name;
                            todo.Source = to.Source;
                            todo.State = 0;
                            todo.UrgencyDegree = 0;
                            todo.UserId = nextUsers[i];
                            list.Add(todo);
                        }
                    }
                }
            }
            try
            {
                ent.ApproalUsres = upUsers;
                ent.NodeCount = upNodeCount;
                var rs = ent.InstanceState == 2 ? service.UpdateAll("DSAS" + bid, 1) : service.Update(new Sys_ToDoList() { Id = todoId, State = 1 });
                if (list != null && list.Count > 0)
                    service.SaveToDoList(list);
                Opinions op = new Opinions()
                {
                    Context = opContext,
                    CreateTime = DateTime.Now,
                    Source = "DSAS" + ent.Id,
                    UserId = userId
                };
                oDao.Add(op);
                var mp = this.dao.Update(ent);
                ReturnModel suc = new ReturnModel();
                suc.SourceId = mp; suc.Message = "操作成功";
                return suc;
            }
            catch (Exception ex)
            {
                ReturnModel err = new ReturnModel();
                err.SourceId = 0; err.Message = ex.Message;
                return err;
            }
        }
    }
}
